package com.example.hybridmobiledemo.module.main.model.shop.classification.shopcart;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hybridmobiledemo.R;
import com.example.hybridmobiledemo.databinding.ActivityShopBinding;
import com.example.hybridmobiledemo.databinding.ActivityShopCartBinding;
import com.example.hybridmobiledemo.module.main.model.shop.bean.ShopCartInfo;
import com.example.hybridmobiledemo.module.main.model.shop.classification.ShopActivity;
import com.example.hybridmobiledemo.util.MyClaDataAdapter;
import com.example.hybridmobiledemo.view.ShopCartRViewAdapter;

import java.util.ArrayList;
import java.util.List;

public class ShopCartActivity extends AppCompatActivity {
    private ActivityShopCartBinding binding;
    private List<ShopCartInfo> cartList;    //从数据库读取购物车数据
    private List<ShopCartInfo> payList = new ArrayList<>();     //结算的数组
    ShopCartRViewAdapter mAdapter;
    LinearLayoutManager mManager;
    private double priceCount;  //总价
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        binding = ActivityShopCartBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initTitle();
        setList();
        mAdapter = new ShopCartRViewAdapter(ShopCartActivity.this, cartList) {
            @Override
            public void onBindViewHolder(@NonNull ShopCartRViewAdapter.ViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                ViewHolder v = (ViewHolder) holder;
//                增加数量
                v.tvShopAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int num = Integer.parseInt(v.tvShopNum.getText().toString()) + 1;
                        if (num > 99) {
                            Toast.makeText(ShopCartActivity.this, "商品数量不能大于99", Toast.LENGTH_SHORT).show();
                        } else {
                            v.tvShopNum.setText(String.valueOf(num));
                            v.cheBoxShop.setChecked(false);
                        }
                    }
                });
//                减少数量
                v.tvShopSub.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int num = Integer.parseInt(v.tvShopNum.getText().toString()) - 1;
                        if (num < 1) {
                            Toast.makeText(ShopCartActivity.this, "商品数量不能小于1", Toast.LENGTH_SHORT).show();
                        } else {
                            v.tvShopNum.setText(String.valueOf(num));
                            v.cheBoxShop.setChecked(false);
                        }
                    }
                });
//                单选控件更新监听
                v.cheBoxShop.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        String sPrice = v.tvShopPrice.getText().toString().substring(4);
                        String sNum = v.tvShopNum.getText().toString();
                        double price = Double.parseDouble(sPrice);
                        int num = Integer.parseInt(sNum);
                        double count = price * num;
                        if (b) {
//                            为结束列表添加数据
                            payList.add(new ShopCartInfo(cartList.get(position).getCommodityId(), cartList.get(position).getCommodityName(), cartList.get(position).getfCostPrice(), cartList.get(position).getNum(), cartList.get(position).getPic()));
//                            总价更改
                            priceCount += count;
                        } else {
//                            移除所添加的数据
                            for(int i=0;i<payList.size();i++){
                                if(payList.get(i).getCommodityId() == cartList.get(position).getCommodityId())
                                    payList.remove(i);
                            }

//                            总价更改
                            priceCount -= count;
                        }
                        if (priceCount < 0) {
//                            总价最小为0
                            priceCount = 0;
                        }
//                        设置为小数点后两位
                        binding.tvShopPriceAll.setText("合计：￥" + String.format("%1.2f", priceCount));
                    }
                });
                v.btnShopDel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                        这是一个删除所有相同的，若删除单个则需另设随机不重复Id
                        new MyClaDataAdapter().delete(ShopCartActivity.this, cartList.get(position).getCommodityId());
                        cartList.remove(position);
                        setList();
                        mAdapter.notifyDataSetChanged();
                    }
                });
            }
        };
        binding.checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    mAdapter.selectAll();
                } else {
                    mAdapter.unSelectAll();
                }
            }
        });
        binding.rvShopList.setAdapter(mAdapter);
        binding.btnShopSettlement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ShopCartActivity.this, "购买成功", Toast.LENGTH_SHORT).show();
//                遍历结算列表实现售出
                for (ShopCartInfo i : payList) {
                    new MyClaDataAdapter().delete(ShopCartActivity.this, i.getCommodityId());
                }
                Intent intent = new Intent(ShopCartActivity.this, ShopActivity.class);
                startActivity(intent);
            }
        });
    }


    private void setList() {
        cartList = new MyClaDataAdapter().QueryAll(ShopCartActivity.this);
        if (cartList.size() != 0) {
            binding.tvShopCartEmpty.setVisibility(View.GONE);
            binding.rvShopList.setVisibility(View.VISIBLE);
            binding.LineLayoutShopPrice.setVisibility(View.VISIBLE);
        } else {
            binding.tvShopCartEmpty.setVisibility(View.VISIBLE);
            binding.rvShopList.setVisibility(View.GONE);
            binding.LineLayoutShopPrice.setVisibility(View.GONE);
        }
        mManager = new LinearLayoutManager(ShopCartActivity.this);
        binding.rvShopList.setLayoutManager(mManager);
    }
    private void initTitle() {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.shop_cart_bar);
        TextView tvShopCartBack = findViewById(R.id.tvShopCartBack);
        ImageView imgShopCartBack = findViewById(R.id.imgShopCartBack);
        tvShopCartBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        imgShopCartBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}